package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.bean.Eat2Bean;
import com.wnhz.work.yz.bean.EatBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---用餐要求
 */
public class EatStandardActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eat_standard);
        getEatInfo();
        initData();

    }

    private List<EatBean.InfoEntity> info1 = new ArrayList<>();

    private void getEatInfo() {
        Map<String, Object> map = new HashMap<>();
        showDialog();
        XUtil.Post(Url.INFORMATION_FOODSTANDARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        EatBean bean = gson.fromJson(result, EatBean.class);
                        info1 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                getEat2Info();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private List<Eat2Bean.InfoEntity> info2 = new ArrayList<>();

    private void getEat2Info() {
        Map<String, Object> map = new HashMap<>();
        XUtil.Post(Url.INFORMATION_FOODPRICESTANDARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        Eat2Bean bean = gson.fromJson(result, Eat2Bean.class);
                        info2 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private ApplyMessagebean.VisitInfoEntity.FoodInfoEntity food_info = new ApplyMessagebean.VisitInfoEntity.FoodInfoEntity();

    private void initData() {
        food_info = ApplyMessageActivity.bean.getVisit_info().getFood_info();
    }

    private TextView tv_eat_standard, tv_eat_type;
    private EditText et_price;

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("用餐要求");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setText("保存");
        tv_right.setTextColor(Color.parseColor("#008c99"));
        tv_right.setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);

        findViewById(R.id.rl_eat_standard).setOnClickListener(this);
        findViewById(R.id.rl_eat_type).setOnClickListener(this);
        //
        tv_eat_standard = (TextView) findViewById(R.id.tv_eat_standard);
        tv_eat_type = (TextView) findViewById(R.id.tv_eat_type);
        et_price = (EditText) findViewById(R.id.et_price);
        if (!"".equals(food_info.getFood_level())) {
            tv_eat_standard.setText(food_info.getFood_level());
            tv_eat_type.setText(food_info.getFood_class());
            et_price.setText(food_info.getFood_price());
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:
//                MyToast("保存");
                String str1 = tv_eat_standard.getText().toString().trim();
                String str2 = tv_eat_type.getText().toString().trim();
                String str3 = et_price.getText().toString().trim();
                if (str1.equals("")) {
                    MyToast("请选择用餐标准");
                    return;
                }
                if (str2.equals("")) {
                    MyToast("请选择用餐类别");
                    return;
                }
                if (str3.equals("")) {
                    MyToast("请填写人均消费");
                    return;
                }
                food_info.setFood_class(str2);
                food_info.setFood_level(str1);
                food_info.setFood_price(str3);
                food_info.setIs_food("1");

                setResult(RESULT_OK);
                finish();
                break;

            //用餐标准
            case R.id.rl_eat_standard: {
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info1.size(); i++) {
                    str.add(info1.get(i).getFood_level());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_eat_standard.setText(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
            }
            break;

            //用餐类别
            case R.id.rl_eat_type: {
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info2.size(); i++) {
                    str.add(info2.get(i).getFood_class());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_eat_type.setText(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
            }
            break;

            default:
                break;

        }
    }
}
